﻿@model  DHQGHN.DAL.Common.DataPage<DHQGHN.PM0.Models.Admin.ApplicationModel>
@{
    var pagNar = ViewBag.Pagination as DHQGHN.DAL.Common.Pagination;
    var lstItem = Model.Data;
}
@using (Html.BeginForm(
                                    actionName: "actionDelete", controllerName: "Application", routeValues: new { }, method: FormMethod.Post, htmlAttributes: new { @id = "apps-form", @class = "form-inline", @role = "form", @style = "width: 100%;" }
                                    ))
{    
    <table class="tbl">
        <thead>
            <tr>
                <th style="width:5%">STT</th>
                <th style="width:5%">Chọn</th>
                <th style="width:30%">Tên ứng dụng</th>
                <th style="width: 15%">Mã ứng dụng</th>
                <th style="width: 40%">Mô tả</th>
                <th style="width:5%">Sửa</th>
                @*<th style="width:5%">Xóa</th>*@
            </tr>
        </thead>
        <tbody id="ctg_content_body">
            @if (Model.Data.Any())
            {
                for (int i = 0; i < lstItem.Count; i++)
                {
                    <tr>
                        <td class="align-center">@(i + 1 + pagNar.CurrentPage * pagNar.PageSize)</td>
                        <td style="text-align:center"><div id="checkboxApps"> <input type="checkbox" name="@lstItem[i].AppId" /></div> </td>
                        <td style="text-align: left;">@lstItem[i].AppName</td>
                        <td style="text-align: left;">@lstItem[i].AppCode</td>
                        <td style="text-align: left;">@lstItem[i].AppDescription</td>                    
                        <td style="text-align: center;">
                            <a title='Sửa thông tin' href='javascript:void(0)' class='app-edit' data-id='@lstItem[i].AppId'>
                                <div class='glyphicon glyphicon-edit'></div>
                            </a>
                        </td>
                        @*<td style="text-align: center;">
                            <a title='Xóa ứng dụng' data-toggle='confirmation' href='javascript:void(0)' class='app-delete' data-id='@lstItem[i].AppId'>
                                <div class='glyphicon glyphicon-trash'></div>
                            </a>
                        </td>*@
                    </tr>
                }
            }
            else
            {
                <tr>
                    <td colspan="6"><p>Không tìm thấy ứng dụng.</p></td>
                </tr>
            }
        </tbody>
        <tfoot>
            <tr>
                <td colspan="6" align="left" style="text-align:left !important">
                    <button type="button" id="btnDelete" style="margin-left: 10px;" class="btn btn-danger"
                            value="Thêm mới">
                        Xóa
                    </button>
                </td>
            </tr>
            <tr>
                <td colspan="6">
                    @if (Model.Data.Any())
                    {
                        @Html.Partial("_PaginationView", pagNar)
                    }
                </td>
            </tr>
        </tfoot>
    </table>
}
<div id="navigate_bottom_panel" class="nav-bottom ">
</div>
<div id="app-model-container"></div>
<script>
        @functions{
            public string GetAntiForgeryToken()
            {
                string cookieToken, formToken;
                AntiForgery.GetTokens(null, out cookieToken, out formToken);
                return cookieToken + "," + formToken;
            }
        }
</script>
<script type="text/javascript">
    var crrPageIndex;
    function OnCriteria(pageIndex) {
        var appname = $('#input-appname');
        var appcode = $('#input-appcode');

        crrPageIndex = pageIndex;
        appname = appname != null ? appname.val().trim() : "";
        appcode = appcode != null ? appcode.val().trim() : "";
        $.ajax({
            dataType: "html",
            type: "POST",
            //type: "GET",
            url: '/Application/actionSearch',
            contentType: "application/json",
            //data: { key: ctrSearch, departmentId: ctrdepartmentId, pageIndex: pageIndex },
            data: JSON.stringify({ pageIndex: pageIndex, aModel: { AppName: appname, AppCode: appcode } }),
            success: function (response) {
                var $catlist = $('#app-list');
                if ($catlist == null || $catlist.length == 0) {
                    window.notice('@*@Resources.Localization.MsgItemNotFound*@', window.notice_error);
                }
                else {
                    $catlist.html(response);
                }

            }
        });
        setTimeout(function () { $('#search_form').find('input:text:visible:first').focus(); }, 500);
    }

    $(document).ready(function () {
        $('a').tipsy();

        $("table.tbl-user-list").colResizable({
            headerOnly: true,
            defaultWith: ["auto", "auto", "auto", "auto", "60px", '30px', '30px', '30px']
        });

        $('.app-edit').bind('click', function (event) {
            var $self = $(this);
            prepareCreateOrUpdate($self.attr('data-id'), 'Cập nhật Ứng dụng');
        });
        
        $('#btnDelete').click(function () {
            var values = [];
            $('#checkboxApps input:checked').each(function () {
                values.push(this.name);
            });

            if (values.length != 0) {
                var $self = $(this);
                comfirmDelete("Bạn có muốn xóa Ứng dụng không?", function () {
                    $.ajax("/Application/actionDelete", {
                        type: "post",
                        contentType: "application/json",
                        data: JSON.stringify({ ids: values }), // JSON data goes here
                        dataType: "json",
                        crossDomain: false,
                        headers: {
                            'VerificationToken': '@GetAntiForgeryToken()'
                        },
                        success: function (response) {
                            if (response.status == 200) {
                                OnCriteria(crrPageIndex);
                                window.notice(response.result, window.notice_success);
                            }
                            else {
                                window.notice(response.result, window.notice_error);
                            }
                        },
                    });
                }, null, "Xóa Ứng dụng");
            } else {
                warning("Cần chọn Ứng dụng để xóa", "Xóa Ứng dụng");
            }
        });       


    });
</script>
